package com.xw.bo.ks.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xw.bo.ks.callback.BaseBeanCallBack;
import com.xw.bo.ks.callback.PageBeanCallBack;
import com.xw.bo.ks.client.RedisClient;
import com.xw.bo.ks.entity.BaseInfo;
import com.xw.bo.ks.entity.UserRegisterInfo;
import com.xw.bo.ks.entity.base.BoPage;
import com.xw.bo.ks.entity.base.HeadDataPage;
import com.xw.bo.ks.entity.common.CodeCommon;
import com.xw.bo.ks.util.XwUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author： XW -> ~
 * @create： 2023/11/9 11:14
 */
public interface BaseIService<T> extends PrintInterface<T> {

    default UserRegisterInfo getUserInfo(RedisClient redisClient,String token) {
        Object objectToken = redisClient.get(token);
        if (null == objectToken) {
            return null;
        }
        if (objectToken instanceof UserRegisterInfo) {
            return (UserRegisterInfo) objectToken;
        }
        return null;
    }

    default void loopDsAccessTokens(BaseBeanCallBack baseBeanCallBack) {
        loopDsAccessTokens(baseBeanCallBack, 1, 100);
    }

    default void loopDsAccessTokens(Wrapper<T> myWrapper, BaseBeanCallBack baseBeanCallBack) {
        loopDsAccessTokens(myWrapper,baseBeanCallBack, 1, 100);
    }

    default void loopDsAccessTokens(BaseBeanCallBack baseBeanCallBack, Integer pageSize) {
        loopDsAccessTokens(baseBeanCallBack, 1, pageSize);
    }

    default void loopDsAccessTokens(BaseBeanCallBack baseBeanCallBack, Integer pageNumber, Integer pageSize) {
        loopDsAccessTokens(new QueryWrapper<>(),baseBeanCallBack, pageNumber, pageSize);
    }

    default void loopDsAccessTokens(Wrapper<T> myWrapper, BaseBeanCallBack baseBeanCallBack, Integer pageNumber, Integer pageSize) {
        HeadDataPage headDataPage = new HeadDataPage();
        headDataPage.setPageNumber(pageNumber);
        headDataPage.setPageSize(pageSize);
        while (true) {
            BoPage<T> dsAccessTokensBoPage = boPageBean(myWrapper, headDataPage);

            if (null == dsAccessTokensBoPage) {
                break;
            }

            if (CollectionUtils.isEmpty(dsAccessTokensBoPage.getList())) {
                break;
            }

            // 将数据扔给回调去处理
            dsAccessTokensBoPage.getList().forEach(baseBeanCallBack::callBack);

            headDataPage.setPageNumber(headDataPage.getPageNumber() + 1);

        }
    }


    default BoPage<T> boPageBean(Wrapper<T> myWrapper, HeadDataPage headDataPage) {
        Page<T> page = PageHelper.startPage(headDataPage.getPageNumberDefault(), headDataPage.getPageSizeDefault());
        List<T> list = findList(myWrapper);
        BoPage<T> pageInfo = new BoPage<>();
        BeanUtils.copyProperties(page, pageInfo);
        pageInfo.setList(list);

        return pageInfo;
    }

    default BoPage<T> boPageBeanBack(Wrapper<T> myWrapper, HeadDataPage headDataPage, PageBeanCallBack<T> pageBeanCallBack) {
        Page<T> page = PageHelper.startPage(headDataPage.getPageNumberDefault(), headDataPage.getPageSizeDefault());
        List<T> list = pageBeanCallBack.findList(myWrapper);
        BoPage<T> pageInfo = new BoPage<>();
        BeanUtils.copyProperties(page, pageInfo);
        pageInfo.setList(list);

        return pageInfo;
    }

    default List<T> findList(Wrapper<T> myWrapper) {
        return list(myWrapper);
    }

    default T findOne(Wrapper<T> myWrapper){
        List<T> list = list(myWrapper);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        if (list.size() > 1){
            printError("findOne error, size > 1");
        }
        return list.get(0);
    }


    default void saveOrUpdateById(T saveT) {
        Long idLong = null;

        saveOrUpdateById(saveT,-1L);
    }

    default void saveOrUpdateById(T saveT, Integer id) {
        Long idLong = null;
        if (null != id) {
            idLong = Long.valueOf(id);
        }
        saveOrUpdateById(saveT,idLong);
    }

    default void saveOrUpdateById(T saveT, Long id) {
        Integer currentTime = (int) (System.currentTimeMillis() / 1000);
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setUpdatedAt(currentTime);
        if (null == saveT) {
            return;
        }
        if (null == id || -1L == id) {
            baseInfo.setIsDelete(CodeCommon.NO_DELETE);
            baseInfo.setCreatedAt(currentTime);
            baseInfo.setCreatedYmd(XwUtil.getDayFormatYmd(System.currentTimeMillis()));
            BeanUtils.copyProperties(baseInfo,saveT);
            save(saveT);
        } else {
            BeanUtils.copyProperties(baseInfo, saveT);
            updateById(saveT);
        }
    }

}
